﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTransferObject;
using BusinessLogicLayer;
using DataAccessLayer;
using Global.Class.Library;

namespace PresentationLayer
{
    public partial class DialogShowMarketingIncentiveDetail : Form
    {
        private MarketingIncentiveDetail marketingIncentiveDetail;
        public ShowMarketingIncentiveDetail showMarketingIncentiveDetail;

        public DialogShowMarketingIncentiveDetail(MarketingIncentiveDetail marketingIncentiveDetail, string warningMessage)
        {
            InitializeComponent();

            this.labelWarningMessage.Text = warningMessage;

            this.marketingIncentiveDetail = marketingIncentiveDetail;
            this.showMarketingIncentiveDetail = new ShowMarketingIncentiveDetail(marketingIncentiveDetail);
        }

        Binding customerIDBinding;
        Binding marketingPaymentTermIDBinding;
        Binding turnOverAmountBinding;
        Binding incentiveAmountBinding;
        Binding paidAmountBinding;
        Binding balanceAmountBinding;
        Binding newTurnOverAmountBinding;
        Binding newIncentiveAmountBinding;
        Binding remarksBinding;

        private void DialogShowMarketingIncentiveDetail_Load(object sender, EventArgs e)
        {
            CommonMetaList commonMetaList = new CommonMetaList();

            ERmgrUIP.ListCustomerNameDataTable listCustomerNameDataTable = commonMetaList.GetCustomerName(false, this.marketingIncentiveDetail.CustomerID);
            this.comboBoxCustomerID.DataSource = listCustomerNameDataTable;
            this.comboBoxCustomerID.DisplayMember = listCustomerNameDataTable.DescriptionOfficialColumn.ColumnName;
            this.comboBoxCustomerID.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;
            this.customerIDBinding = this.comboBoxCustomerID.DataBindings.Add("SelectedValue", this.marketingIncentiveDetail, "CustomerID", true);


            ERmgrUIP.ListMarketingPaymentTermDataTable listMarketingPaymentTermDataTable = commonMetaList.GetMarketingPaymentTerm(false);
            this.comboBoxMarketingPaymentTermID.DataSource = listMarketingPaymentTermDataTable;
            this.comboBoxMarketingPaymentTermID.DisplayMember = listMarketingPaymentTermDataTable.DescriptionColumn.ColumnName;
            this.comboBoxMarketingPaymentTermID.ValueMember = listMarketingPaymentTermDataTable.MarketingPaymentTermIDColumn.ColumnName;
            this.marketingPaymentTermIDBinding = this.comboBoxMarketingPaymentTermID.DataBindings.Add("SelectedValue", this.marketingIncentiveDetail, "MarketingPaymentTermID", true);


            this.turnOverAmountBinding = this.textBoxTurnOverAmount.DataBindings.Add("Text", this.marketingIncentiveDetail, "TurnOverAmount", true); this.turnOverAmountBinding.FormatString = "N0";
            this.incentiveAmountBinding = this.textBoxIncentiveAmount.DataBindings.Add("Text", this.marketingIncentiveDetail, "IncentiveAmount", true); this.incentiveAmountBinding.FormatString = "N0";
            this.paidAmountBinding = this.textBoxPaidAmount.DataBindings.Add("Text", this.showMarketingIncentiveDetail, "PaidAmount", true); this.paidAmountBinding.FormatString = "N0";
            this.balanceAmountBinding = this.textBoxBalanceAmount.DataBindings.Add("Text", this.showMarketingIncentiveDetail, "BalanceAmount", true); this.balanceAmountBinding.FormatString = "N0";
            this.newTurnOverAmountBinding = this.textBoxNewTurnOverAmount.DataBindings.Add("Text", this.showMarketingIncentiveDetail, "NewTurnOverAmount", true, DataSourceUpdateMode.OnPropertyChanged); this.newTurnOverAmountBinding.FormatString = "N0";
            this.newIncentiveAmountBinding = this.textBoxNewIncentiveAmount.DataBindings.Add("Text", this.showMarketingIncentiveDetail, "NewIncentiveAmount", true, DataSourceUpdateMode.OnPropertyChanged); this.newIncentiveAmountBinding.FormatString = "N0";
            this.remarksBinding = this.textBoxRemarks.DataBindings.Add("Text", this.marketingIncentiveDetail, "Remarks", true); this.remarksBinding.FormatString = "N0";


            this.checkBoxInValid.DataBindings.Add("Checked", this.toolStripButtonOK, "Enabled", true, DataSourceUpdateMode.OnPropertyChanged);
            this.checkBoxInValid.DataBindings.Add("CheckState", this.showMarketingIncentiveDetail, "InValid", true, DataSourceUpdateMode.OnPropertyChanged);


            this.customerIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marketingPaymentTermIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.turnOverAmountBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.incentiveAmountBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.paidAmountBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.balanceAmountBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.newTurnOverAmountBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.newIncentiveAmountBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.remarksBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.errorProviderMaster.DataSource = this.showMarketingIncentiveDetail;
        }

        private void CommonControl_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState == BindingCompleteState.Exception) { GlobalExceptionHandler.ShowExceptionMessageBox(this, e.ErrorText); e.Cancel = true; }
        }

        private void toolStripButtonEscapeAndOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = sender.Equals(this.toolStripButtonOK) ? System.Windows.Forms.DialogResult.Yes : System.Windows.Forms.DialogResult.No;
        }

    }

    public class ShowMarketingIncentiveDetail : NotifyPropertyChangeObject
    {
        private MarketingIncentiveDetail marketingIncentiveDetail;

        private double paidAmount;
        private double newTurnOverAmount;
        private double newIncentiveAmount;

        public ShowMarketingIncentiveDetail(MarketingIncentiveDetail marketingIncentiveDetail)
        {
            this.marketingIncentiveDetail = marketingIncentiveDetail;

            this.PaidAmount = SQLDatabase.GetScalarValue("SELECT TOP 1 PaidAmount FROM MarketingIncentiveDetail WHERE MarketingIncentiveID = " + this.marketingIncentiveDetail.MarketingIncentiveID + " AND SerialID = " + this.marketingIncentiveDetail.SerialID);

            this.NewTurnOverAmount = this.marketingIncentiveDetail.TurnOverAmount;
            this.NewIncentiveAmount = this.marketingIncentiveDetail.IncentiveAmount;
        }

        public bool InValid { get { return !this.IsValid; } }

        [DefaultValue(0)]
        public double PaidAmount
        {
            get { return this.paidAmount; }
            set { ApplyPropertyChange<ShowMarketingIncentiveDetail, double>(ref this.paidAmount, o => o.PaidAmount, Math.Round(value, GlobalVariables.Round0Amount)); }
        }

        [DefaultValue(0)]
        public double NewTurnOverAmount
        {
            get { return this.newTurnOverAmount; }
            set { ApplyPropertyChange<ShowMarketingIncentiveDetail, double>(ref this.newTurnOverAmount, o => o.NewTurnOverAmount, Math.Round(value, GlobalVariables.Round0Amount)); }
        }

        [DefaultValue(0)]
        public double NewIncentiveAmount
        {
            get { return this.newIncentiveAmount; }
            set { ApplyPropertyChange<ShowMarketingIncentiveDetail, double>(ref this.newIncentiveAmount, o => o.NewIncentiveAmount, Math.Round(value, GlobalVariables.Round0Amount)); }
        }

        public double BalanceAmount { get { return this.marketingIncentiveDetail.IncentiveAmount - this.PaidAmount; } }

        
        protected override System.Collections.Generic.List<ValidationRule> CreateRules()
        {
            List<ValidationRule> validationRules = base.CreateRules();
            validationRules.Add(new SimpleValidationRule("NewTurnOverAmount", "Sales amount has changed.", delegate { return this.NewTurnOverAmount == this.marketingIncentiveDetail.TurnOverAmount; }));
            validationRules.Add(new SimpleValidationRule("NewIncentiveAmount", "Incentive amount has changed.", delegate { return this.NewIncentiveAmount == this.marketingIncentiveDetail.IncentiveAmount; }));

            return validationRules;
        }
    }
}
